package io.yuncheng.modules.sys.service.impl;

import io.yuncheng.common.utils.Constant;
import io.yuncheng.modules.sys.entity.SubApplicationEntity;
import io.yuncheng.modules.sys.service.SubApplicationService;
import io.yuncheng.modules.sys.dao.SubAppAssessmentDao;
import io.yuncheng.modules.sys.entity.SubAppAssessmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.yuncheng.common.utils.PageUtils;
import io.yuncheng.common.utils.Query;

import io.yuncheng.modules.sys.service.SubAppAssessmentService;


@Service("subAppAssessmentService")
public class SubAppAssessmentServiceImpl extends ServiceImpl<SubAppAssessmentDao, SubAppAssessmentEntity> implements SubAppAssessmentService {

    @Autowired
    private SubApplicationService applicationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Object year = params.get("assessmentDate");
        Object appId = params.get("appId");
        Object status = params.get("status");
        Object notStatus = params.get("notStatus");
        IPage<SubAppAssessmentEntity> page = this.page(
                new Query<SubAppAssessmentEntity>().getPage(params),
                new QueryWrapper<SubAppAssessmentEntity>().eq(year!=null,"assessment_date",year)
                .eq(appId!=null,"app_id",appId)
                .eq(status != null,"status",status)
                .notIn(notStatus != null,"status",notStatus)
        );

        return new PageUtils(page);
    }

    /**
     * 参数转化
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageConfirm(Map<String, Object> params) {
        Long dept = null;
        Object deptId = params.get("deptId");
        if(deptId!=null){
            dept = Long.valueOf(deptId.toString());
        }

        Object year = params.get("year");
        Integer  years = null;
        if(year!=null){
            years = Integer.valueOf(year.toString());
        }
       return queryByDeptYearStatus(dept,years,null,Constant.StatusType.OFF.getValue());
    }

    @Override
    public PageUtils queryByDeptYearStatus(Long deptId, Integer year, Integer status, Integer noStatus) {
        Map<String, Object> paramsPage = new HashMap<>(4);
        if (deptId != null) {
            SubApplicationEntity subApplicationEntity = applicationService.queryByDeptId(Long.valueOf(deptId.toString()));
            paramsPage.putIfAbsent("appId", subApplicationEntity.getAppId());
        }
        if (year != null) {
            paramsPage.putIfAbsent("assessmentDate", new Date((int) year - 1900, 0, 1));
        }
        if(status!=null){
            paramsPage.put("status",status);
        }
        if(noStatus!=null){
            paramsPage.put("noStatus",noStatus);
        }
        PageUtils pageUtils = queryPage(paramsPage);
        return pageUtils;
    }
}
